<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.${className}Mapper">

    <sql id="Base_column_list">
    <#list attrs as attr>
    <#if !attr_has_next>
        ${attr.dbName} as ${attr.name}
    <#else>
        ${attr.dbName} as ${attr.name},
    </#if>
    </#list>
    </sql>

    <select id="selectByPrimaryKey" resultType="${entityPath}.${className}">
        SELECT
        <include refid="Base_column_list"/>
        FROM
    `${tableName}`
        WHERE `${key}` = ${r"#{"}${val}}
    </select>

    <select id="list" resultType="${entityPath}.${className}">
        SELECT
        <include refid="Base_column_list"/>
        FROM `${tableName}`
        <where>
        <#list attrs as attr>
            <if test="${attr.name} != null"> AND `${attr.dbName}` = ${r"#{"}${attr.name}}</if>
        </#list>
        </where>
    </select>

    <select id="count" resultType="int">
        SELECT count(*) FROM `${tableName}`
        <where>
        <#list attrs as attr>
            <if test="${attr.name} != null"> AND `${attr.dbName}` = ${r"#{"}${attr.name}}</if>
        </#list>
        </where>
    </select>

    <insert id="insert" parameterType="${entityPath}.${className}" useGeneratedKeys="true" keyProperty="${val}">
    INSERT INTO `${tableName}`
    (
    <#list attrs as attr>
        <#if !attr_has_next>
            `${attr.dbName}`
        <#else>
            `${attr.dbName}`,
        </#if>
    </#list>
    )
    VALUES
    (
    <#list attrs as attr>
        <#if !attr_has_next>
            ${r"#{"}${attr.name}}
        <#else>
            ${r"#{"}${attr.name}},
        </#if>
    </#list>
    )
    </insert>

    <insert id="batchInsert" parameterType="${entityPath}.${className}">
        REPLACE INTO `${tableName}` (
        <#list attrs as attr>
            <#if !attr_has_next>
                `${attr.dbName}`
            <#else>
                `${attr.dbName}`,
            </#if>
        </#list>
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            <#list attrs as attr>
                <#if !attr_has_next>
                    ${r"#{item."}${attr.name}}
                <#else>
                    ${r"#{item."}${attr.name}},
                </#if>
            </#list>
            )
        </foreach>
    </insert>

    <update id="updateByPrimaryKey" parameterType="${entityPath}.${className}">
        UPDATE `${tableName}`
        <set>
    <#list attrs as attr>
        <#if !attr_has_next>
            <if test="${attr.name} != null AND ${attr.name} != ''"> `${attr.dbName}` = ${r"#{"}${attr.name}}</if>
        <#else>
            <if test="${attr.name} != null AND ${attr.name} != ''"> `${attr.dbName}` = ${r"#{"}${attr.name}},</if>
        </#if>
    </#list>
        </set>
        where `${key}` = ${r"#{"}${val}}
    </update>

    <update id="batchUpdate" parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
        update `${tableName}`
        <set>
        <#list attrs as attr>
        <#if !attr_has_next>
        `${attr.dbName}`=${r"#{item."}${attr.name}}
        <#else>
        `${attr.dbName}`=${r"#{item."}${attr.name}},
        </#if>
        </#list>
        </set>
        where ${key} = ${r"#{item."}${val}}
        </foreach>
    </update>

    <delete id="deleteByPrimaryKey">
        DELETE FROM `${tableName}` where `${key}` = ${r"#{"}${val}}
    </delete>

    <delete id="batchRemove">
        DELETE FROM `${tableName}` where `${key}` in
        <foreach item="item" collection="array" open="(" separator="," close=")">
        ${r"#{item"}}
        </foreach>
    </delete>

</mapper>